package cn;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Date;

/**
 * 爬虫的数据持久化组件
 * 调用dao  把数据添加到数据库
 */
public class DataDao extends BaseDaoJdbc implements Pipeline {

    //爬虫数据持久化  sql
    private  String sql = "INSERT INTO information(title,author,content,type,time,source,readtotal) VALUES(?,?,?,?,?,?,?)";
    private  String sqltopic = "INSERT INTO topic(typename) VALUES(?)";
    private String sqlgetID = "SELECT * FROM topic WHERE typename = ? ";

    /**
     * resultItems 获取ReptileMain组件解析后数据 page.putField("bj",bj.text());
     * @param resultItems
     * @param task
     */
    public void process(ResultItems resultItems, Task task) {
        Object bj = resultItems.get("bj");
        Object currChnlCls = resultItems.get("currChnlCls");
        Object title_tex = resultItems.get("title_tex");
        Object time_tex = resultItems.get("time_tex");
        Object time_ly = resultItems.get("time_ly");
        Object trs_editor = resultItems.get("trs_editor");//内容

        System.out.println("bj="+bj+"currChnlCls="+currChnlCls
        +"title_tex="+title_tex
        +"time_tex"+time_tex
        +"time_ly="+time_ly
        +"trs_editor="+trs_editor);

        try{
            //获取信息的类型数据currChnlCls  对字符串
            String s = currChnlCls.toString();
            String[] s1 = s.split(" ");

            //查询数据库  返回id
            int id = queryToID(sqlgetID, s1[s1.length-1]);
            if(id==0){
                //有数据
                //保存到数据库   topic  表
                update(sqltopic,s1[s1.length-1]);
                id = queryToID(sqlgetID, s1[s1.length-1]);
            }
            //把数据添加information
            //获取  topic ID
            System.out.println(id);
            //处理数据
            String[] author = bj.toString().split(":");
            String[] source = time_ly.toString().split(":");

            int update = update(sql, title_tex.toString(),
                    author[author.length-1],
                    trs_editor.toString(),
                    id,
                    new Date(),
                    source[source.length-1], 0);

//            update(sql,"标题","作者","内容",1,new Date(),"四川花园",1);
//            title,author,content,type,time,source,readtotal
            System.out.println("update--"+update);

        }catch (Exception e){
//            e.printStackTrace();
        }



    }
}
